﻿using System;
using System.Threading;

namespace Devme.Diagnostics.Examples
{
    class Program
    {
        static void Main()
        {
            Example1();
        	Console.WriteLine();
            Example2();
        	Console.WriteLine();
            Example3();

        	Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

		/// <summary>
		/// Example 1 (Simple)
		/// Console output:
		///		Elapsed: 00:00:02.345
		/// </summary>
		static void Example1()
		{
			Console.WriteLine("Example 1 (Simple)...");
			using (StopwatchManager.Start(elapsed => Console.WriteLine("Elapsed: {0}", elapsed)))
			{
				Thread.Sleep(2345);
			}
		}

		/// <summary>
		/// Example 2 (Named regions)
		/// Console output:
		///		First.  Elapsed: 00:00:01
		///		Second. Elapsed: 00:00:02
		///		Third.  Elapsed: 00:00:00.500, Total: 00:00:03.500, Count: 3, Min: 00:00:00.500, Max: 00:00:02.000, Avg: 00:00:01.166
		/// </summary>
		static void Example2()
		{
			Console.WriteLine("Example 2 (Named regions)...");

			using (StopwatchManager.Start("region1", elapsed => Console.WriteLine("First. Elapsed: {0}", elapsed.Current)))
			{
				Thread.Sleep(1000);
			}
			using (StopwatchManager.Start("region1", elapsed => Console.WriteLine("Second. Elapsed: {0}", elapsed.Current)))
			{
				Thread.Sleep(2000);
			}
			using (StopwatchManager.Start("region1", elapsed => Console.WriteLine("Third. " +
																				  "Elapsed: {0}, " +
																				  "Total: {1}, " +
																				  "Count: {2}, " +
																				  "Min: {3}, " +
																				  "Max: {4}, " +
																				  "Avg: {5}",
																				  elapsed.Current,
																				  elapsed.Total,
																				  elapsed.Count,
																				  elapsed.Min,
																				  elapsed.Max,
																				  elapsed.Avg
																	)))
			{
				Thread.Sleep(500);
			}
		}

		/// <summary>
		/// Example 3 (Hierarchy)
		/// Console output:
		///		Operation1: 00:00:00.700
		///		Operation3: 00:00:00.500
		///		Operation4: 00:00:00.100
		///		Total: 00:00:01.500
		/// </summary>
    	static void Example3()
    	{
    		Console.WriteLine("Example 3 (Hierarchy)...");
    		using (StopwatchManager.Start(elapsed => Console.WriteLine("Total: {0}", elapsed)))
    		{
    			using (StopwatchManager.Start(elapsed => Console.WriteLine("Operation1: {0}", elapsed)))
    			{
    				Thread.Sleep(700); // Operation1
    			}

    			Thread.Sleep(200); // Operation2

    			using (StopwatchManager.Start(elapsed => Console.WriteLine("Operation3: {0}", elapsed)))
    			{
    				Thread.Sleep(500); // Operation3
    				using (StopwatchManager.Start(elapsed => Console.WriteLine("Operation4: {0}", elapsed)))
    				{
    					Thread.Sleep(100); // Operation3
    				}
    			}
    		}
    	}
    }
}